/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.semantic;

import java.util.Iterator;

import alloy.ast.ASTDepthFirstReplacer;
import alloy.ast.BinaryExpr;
import alloy.ast.BinaryExprOp;
import alloy.ast.Expr;
import alloy.ast.ModuleInfo;
import alloy.ast.Signature;
import alloy.ast.VariableExpr;
import alloy.type.UnionType;


/**
 * This visitor looks at each field reference and replaces it with a BinaryExpr which
 * is a join of a given Expr and the field reference iff:  (1) the type of the field's
 * first column intersects the type of the given Expr, and (2) the reference is not 
 * prefixed with the "@" sign. 
 **/
class FieldRefExpandingVisitor extends ASTDepthFirstReplacer {
    
    private Expr _expr;
    private ModuleInfo _modInfo;
    
    /**
     * @param expr_ the expression that will be joined (from the left) to every
     * field reference encountered during subtree traversal.  The getType() method
     * of expr_ must return an instance of UnionType
     * @param modInfo_ the ModuleInfo which contains the mapping from all fields
     * to the Signatures that contain them
     **/
    public FieldRefExpandingVisitor(Expr expr_, ModuleInfo modInfo_) {
	_expr = expr_;
	_modInfo = modInfo_;
    }
    
    /**
     * Visit a variable expr.  Transform it into a binary expression of the form
     * _expr.variableExpr if variableExpr is not prefixed with "@".
     * @return transformed VariableExpr
     */
    public Object visit(VariableExpr varExpr_) {
	// it's not a field, or is flagged, so do nothing .. .
	if (varExpr_.isQuantified || varExpr_.getVar().isFlagged() ||
	    !_isFieldOfSig(varExpr_.getVar().getId().nodeString()))
	    return varExpr_;
	else 
	    return new BinaryExpr((Expr) _expr.copy(), 
				  BinaryExprOp.JOIN_DOT, 
				  varExpr_);
    }

    /**
     * @return true if field_ is a field of sig or any of its subtypes/supertypes
     **/
    private boolean _isFieldOfSig(String field_) {
	try {
	    Iterator sigIter = _modInfo.getSigsForField(field_).iterator();
	    UnionType exprType = _expr.getType();
	    while (sigIter.hasNext()) {
		UnionType curType = ((Signature) sigIter.next()).getUnionType();
		if ((exprType.arity() == curType.arity()) && (!exprType.intersect(curType).isEmpty()))
		    return true;
	    }
	} catch (ModuleInfo.NoSigsException nse) {} // nothing to do, false will be returned

	return false;
 
    }
}
